home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadwin / support / attredef.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1995-02-08  |  11.8 KB  |  345 lines

  1. ; Next available MSG number is    13 
  2. ; MODULE_ID ATTREDEF_LSP_
  3. ;;;
  4. ;;;    attredef.lsp
  5. ;;;
  6. ;;;    Copyright (C) 1988, 1990, 1992, 1994 by Autodesk, Inc.
  7. ;;;
  8. ;;;    Permission to use, copy, modify, and distribute this software
  9. ;;;    for any purpose and without fee is hereby granted, provided
  10. ;;;    that the above copyright notice appears in all copies and
  11. ;;;    that both that copyright notice and the limited warranty and
  12. ;;;    restricted rights notice below appear in all supporting
  13. ;;;    documentation.
  14. ;;;
  15. ;;;    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
  16. ;;;    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
  17. ;;;    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18. ;;;    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
  19. ;;;    UNINTERRUPTED OR ERROR FREE.
  20. ;;;
  21. ;;;    Use, duplication, or disclosure by the U.S. Government is subject to
  22. ;;;    restrictions set forth in FAR 52.227-19 (Commercial Computer
  23. ;;;    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24. ;;;    (Rights in Technical Data and Computer Software), as applicable.
  25. ;;;
  26. ;;;.
  27. ;;;
  28. ;;; DESCRIPTION
  29. ;;;
  30. ;;;   This program allows you to redefine a Block and update the
  31. ;;;   Attributes associated with any previous insertions of that Block.
  32. ;;;   All new Attributes are added to the old Blocks and given their
  33. ;;;   default values. All old Attributes with equal tag values to the new
  34. ;;;   Attributes are redefined but retain their old value. And all old
  35. ;;;   Attributes not included in the new Block are deleted.
  36. ;;;
  37. ;;;   Note that if handles are enabled, new handles will be assigned to
  38. ;;;   each redefined block.
  39. ;;;
  40. ;;; --------------------------------------------------------------------------;
  41.  
  42. ;;;
  43. ;;; Oldatts sets "old_al" (OLD_Attribute_List) to the list of old Attributes
  44. ;;; for each Block.  The list does not include constant Attributes.
  45. ;;;
  46. (defun oldatts (/ e_name e_list cont)
  47.   (setq oa_ctr 0 
  48.         cont   T
  49.         e_name b1
  50.   )
  51.   (while cont
  52.     (if (setq e_name (entnext e_name))
  53.       (progn
  54.         (setq e_list (entget e_name))
  55.         (if (and (= (cdr (assoc 0 e_list)) ;|MSG0|;"ATTRIB")
  56.                  ;; NOT a constant attribute -- (cdr (assoc 70 e_list)) != 2)
  57.                  (/= (logand (cdr (assoc 70 e_list)) 2) 2))
  58.           (progn
  59.             (if old_al
  60.               (setq old_al (cons e_list old_al))
  61.               (setq old_al (list e_list))
  62.             )
  63.             (setq oa_ctr (1+ oa_ctr))           ; count the number of old atts
  64.           )
  65.           ;; else, exit
  66.           (setq cont nil)
  67.         )
  68.       )
  69.       (setq cont nil)
  70.     )
  71.   )
  72. )
  73. ;;;
  74. ;;; Newatts sets "new_al" to the list of new Attributes in the new Block.
  75. ;;; The list does not include constant Attributes.
  76. ;;;
  77. (defun newatts (ssetn ssl / i e_name e_list)
  78.   (setq i 0 na_ctr 0)
  79.   (while (< i ssl)
  80.     (if (setq e_name (ssname ssetn i))
  81.       (progn
  82.         (setq e_list (entget e_name))
  83.         (if (and (= (cdr (assoc 0 e_list)) ;|MSG0|;"ATTDEF")
  84.                  ;; NOT a constant attribute -- (cdr (assoc 70 e_list)) != 2)
  85.                  (/= (logand (cdr (assoc 70 e_list)) 2) 2))
  86.           (progn
  87.             (if new_al
  88.               (setq new_al (cons e_list new_al))
  89.               (setq new_al (list e_list))
  90.             )
  91.             (setq na_ctr (1+ na_ctr))     ; count the number of new atts
  92.           )
  93.         )
  94.       )
  95.     )
  96.     (setq i (1+ i))
  97.   )
  98.   na_ctr
  99. )
  100. ;;;
  101. ;;; Compare the list of "old" to the list of "new" Attributes and make
  102. ;;; the two lists "same" and "preset". "Same" contains the old values of
  103. ;;; all the Attributes in "old" with equal tag values to some Attribute
  104. ;;; in "new" and the default values of all the other Attributes. "Preset"
  105. ;;; contains the preset Attributes in old with equal tag values to some
  106. ;;; Attribute in new.
  107. ;;;
  108. (defun compare (/ i j)
  109.   (setq i 0
  110.         j 0
  111.         pa_ctr 0
  112.         same nil
  113.         va_ctr 0
  114.         preset nil)
  115.   ;; "i" is a counter that increments until the number of new attributes
  116.   ;; is reached.
  117.   (while (< i na_ctr)
  118.     (cond 
  119.       ;; If there are old attributes AND the tag strings of the old and new 
  120.       ;; attributes are the same...
  121.       ((and old_al
  122.             (= (cdr (assoc 2 (nth j old_al))) (cdr (assoc 2 (nth i new_al)))))
  123.         ;; IS a preset attribute -- (cdr (assoc 70 e_list)) == 8)
  124.         (if (= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  125.           ;; If the attribute is a preset attribute then add it to the list
  126.           ;; of preset attributes and increment the counter "pa_ctr".
  127.           ;; IS a preset attribute -- (cdr (assoc 70 e_list)) == 8)
  128.           (progn
  129.             (if preset
  130.               (setq preset (cons (nth j old_al) preset))
  131.               (setq preset (list (nth j old_al)))
  132.             )
  133.             (setq pa_ctr (1+ pa_ctr))     ; count preset atts
  134.           )
  135.           ;; Else, add it to the list of same attributes "same".
  136.           (if same
  137.             (setq same (cons (cdr (assoc 1 (nth j old_al))) same))
  138.             (setq same (list (cdr (assoc 1 (nth j old_al)))))
  139.           )
  140.         )
  141.         ;; If the attribute must be verified, increment counter "va_ctr".
  142.         ;; NOT a preset attribute -- (cdr (assoc 70 e_list)) != 8)
  143.         (if (and (/= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  144.                  ;; IS a verified attribute -- (cdr (assoc 70 e_list)) == 4)
  145.                  (= (logand (cdr (assoc 70 (nth i new_al))) 4) 4))
  146.           (setq va_ctr (+ 1 va_ctr))
  147.         )
  148.         (setq i (1+ i))
  149.         (setq j 0)
  150.       )
  151.       ;; If the number of old attributes equals the old attribute counter "j"
  152.       ((= j oa_ctr)
  153.         ;; If this attribute is not a preset attribute, but is not in the 
  154.         ;; old list, then add it to the list "same".
  155.         ;; NOT a preset attribute -- (cdr (assoc 70 e_list)) != 8)
  156.         (if (/= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  157.           (if same
  158.             (setq same (cons (cdr (assoc 1 (nth i new_al))) same))
  159.             (setq same (list (cdr (assoc 1 (nth i new_al)))))
  160.           )
  161.         )
  162.         ;; NOT a preset attribute -- (cdr (assoc 70 e_list)) != 8)
  163.         (if (and (/= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  164.                  ;; IS a verified attribute -- (cdr (assoc 70 e_list)) == 4)
  165.                  (= (logand (cdr (assoc 70 (nth i new_al))) 4) 4))
  166.           (setq va_ctr (+ 1 va_ctr))
  167.         )
  168.         (setq i (1+ i))
  169.         (setq j 0)
  170.       )
  171.       ;; Increment the old attribute counter "j"...
  172.       (t
  173.         (setq j (1+ j))
  174.       )
  175.     )
  176.   )
  177. )
  178. ;;;
  179. ;;; Find the entity for each of the "preset" Attributes in the newly
  180. ;;; inserted Block.
  181. ;;;
  182. (defun findpt ()
  183.   (setq test T)
  184.   (setq en (entnext e1))
  185.   (setq e_list (entget en))
  186.   (while test
  187.     (if (and (= (cdr (assoc 0 e_list)) ;|MSG0|;"ATTRIB") (= (cdr (assoc 2 e_list)) tag))
  188.       (setq test nil)
  189.       (progn
  190.         (setq ex en)
  191.         (setq en (entnext ex))
  192.         (if e_list
  193.           (setq e_list (entget en))
  194.         )
  195.       )
  196.     )
  197.   )
  198. )
  199. ;;;
  200. ;;; Insert a new Block on top of each old Block and set its new Attributes
  201. ;;; to their values in the list "same". Then replace each of the "preset"
  202. ;;; Attributes with its old value.
  203. ;;;
  204. (defun redef (/ xsf ysf zsf ls i e1 v)
  205.   (command "_.UCS" "_E" b1)         ; define the block's UCS
  206.   (setq xsf (cdr (assoc 41 (entget b1)))) ; find x scale factor
  207.   (setq ysf (cdr (assoc 42 (entget b1)))) ; find y scale factor
  208.   (setq zsf (cdr (assoc 43 (entget b1)))) ; find z scale factor
  209.   (setq ls (length same))
  210.   (setq i 0)
  211.   (command "_.INSERT" bn "0.0,0.0,0.0" "_XYZ" xsf ysf zsf "0.0")
  212.   (while (< i ls)                     ; set attributes to their values
  213.     (command (nth i same))
  214.     (setq i (1+ i))
  215.   )
  216.   (while (< 0 va_ctr)
  217.     (command "")                      ; at prompts, verify attributes
  218.     (setq va_ctr (1- va_ctr))
  219.   )
  220.   (setq i 0)
  221.   (setq e1 (entlast))
  222.   (while (< 0 pa_ctr)                    ; edit each of the "preset" attributes
  223.     (setq tag (cdr (assoc 2 (nth i preset))))
  224.     (setq v (cdr (assoc 1 (nth i preset))))
  225.     (findpt)                          ; find the entity to modify
  226.     (setq e_list (subst (cons 1 v) (assoc 1 e_list) e_list))
  227.     (entmod e_list)                        ; modify the entity's value
  228.     (setq i (1+ i))
  229.     (setq pa_ctr (1- pa_ctr))
  230.   )
  231.   (command "_.UCS" "_P")                 ; restore the previous UCS
  232. )
  233. ;;;
  234. ;;; System variable save
  235. ;;;
  236. (defun modes (a)
  237.   (setq mlst '())
  238.   (repeat (length a)
  239.     (setq mlst (append mlst (list (list (car a) (getvar (car a))))))
  240.     (setq a (cdr a)))
  241. )
  242. ;;;
  243. ;;; System variable restore
  244. ;;;
  245. (defun moder ()
  246.   (repeat (length mlst)
  247.     (setvar (caar mlst) (cadar mlst))
  248.     (setq mlst (cdr mlst))
  249.   )
  250. )
  251. ;;;
  252. ;;; Internal error handler
  253. ;;;
  254. (defun attrerr (s)                    ; If an error (such as CTRL-C) occurs
  255.                                       ; while this command is active...
  256.   (if (/= s "Funci≤n cancelada")
  257.     (princ (strcat "\nError: " s))
  258.   )
  259.   (moder)                             ; restore saved modes
  260.   (setq *error* olderr)               ; restore old *error* handler
  261.   (princ)
  262. )
  263. ;;;
  264. ;;; Main program
  265. ;;;
  266. (defun C:ATTREDEF (/ k n olderr bn sseto ssetn pt ssl new_al
  267.                      old_al same preset b1 oa_ctr va_ctr na_ctr
  268.                   ) 
  269.   (setq k 0
  270.       n 0
  271.       test T
  272.       olderr *error*
  273.       *error* attrerr
  274.   )
  275.  
  276.   (modes '("CMDECHO" "ATTDIA" "ATTREQ" "GRIDMODE" "UCSFOLLOW"))
  277.   (setvar "cmdecho" 0)                ; turn cmdecho off
  278.   (setvar "attdia" 0)                 ; turn attdia off
  279.   (setvar "attreq" 1)                 ; turn attreq on
  280.   (setvar "gridmode" 0)               ; turn gridmode off
  281.   (setvar "ucsfollow" 0)              ; turn ucsfollow off
  282.  
  283.   (while 
  284.     (progn
  285.       (setq bn (xstrcase (getstring 
  286.         "\nNombre del bloque que se quiere redefinir: ")))
  287.       (if (tblsearch ;|MSG0|;"block" bn)
  288.         (progn
  289.           (setq sseto (ssget ;|MSG0|;"_x" (list (cons 2 bn))))
  290.           (setq test nil)
  291.         )
  292.         (progn
  293.           (princ "\nEl bloque ")
  294.           (princ bn)
  295.           (princ " no estß definido. Por favor, intΘntelo de nuevo.\n")
  296.         )
  297.        )
  298.     )
  299.   )
  300.   (if sseto
  301.     (progn
  302.       (while 
  303.         (progn
  304.           (princ "\nDesigne objetos para el nuevo bloque... ")
  305.           (if (null (setq ssetn (ssget)))
  306.             (princ "\nNo se ha seleccionado ning·n bloque nuevo. IntΘntelo de nuevo.")
  307.             (setq test nil)
  308.           )
  309.         )
  310.       )
  311.       ;; find the list of new attributes
  312.       (setq na_ctr (newatts ssetn (sslength ssetn)) )
  313.       (if (> na_ctr 0)
  314.         (progn
  315.           (initget 1)
  316.           (setq pt (getpoint "\nPunto de inserci≤n del nuevo bloque: "))
  317.           (setq ssl (sslength sseto))
  318.           ;; redefine the block
  319.           (command "_.BLOCK" bn ;|MSG0|;"_Y" pt ssetn "") 
  320.           (while (< k ssl)
  321.             (setq b1 (ssname sseto k))    ; For each old block...
  322.             (setq old_al nil)
  323.             (oldatts)                     ; find the list of old attributes,
  324.             (compare)                     ; compare the old list with the new,
  325.             (redef)                       ; and redefine its attributes.
  326.             (entdel b1)                   ; delete the old block.
  327.             (setq k (1+ k))
  328.           )
  329.           (command "_.REGENALL")
  330.         )
  331.         (princ "\nEl nuevo bloque no tiene atributos. ")
  332.       )
  333.     )
  334.     (princ (strcat "\nNo hay inserciones de bloque " bn " para redefinir. "))
  335.   )
  336.   (moder)                             ; restore saved modes
  337.   (setq *error* olderr)               ; restore old *error* handler
  338.   (princ)
  339. )
  340.  
  341. (defun c:at () (c:attredef))
  342. (princ 
  343. "\nC:ATtredef cargada. Active el comando con AT o ATTREDEF.")
  344. (princ)
  345.